<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->
<!--      <el-form-item label="门槛" prop="menkan">-->
<!--        <el-input-->
<!--          v-model="queryParams.menkan"-->
<!--          placeholder="请输入门槛"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="利润" prop="profit">-->
<!--        <el-input-->
<!--          v-model="queryParams.profit"-->
<!--          placeholder="请输入利润"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="塔层数" prop="level">-->
<!--        <el-input-->
<!--          v-model="queryParams.level"-->
<!--          placeholder="请输入塔层数"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="排序值" prop="sort">-->
<!--        <el-input-->
<!--          v-model="queryParams.sort"-->
<!--          placeholder="请输入排序值"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="门票价格" prop="price">-->
<!--        <el-input-->
<!--          v-model="queryParams.price"-->
<!--          placeholder="请输入门票价格"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="塔热度" prop="hotkey">-->
<!--        <el-input-->
<!--          v-model="queryParams.hotkey"-->
<!--          placeholder="请输入塔热度"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="区间 如果设置为 0 则表示不设置区间 有数字则表示间隔多少发出一次额外奖" prop="goodsInterval">-->
<!--        <el-input-->
<!--          v-model="queryParams.goodsInterval"-->
<!--          placeholder="请输入区间 如果设置为 0 则表示不设置区间 有数字则表示间隔多少发出一次额外奖"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="是否显示 默认0 不公开显示" prop="ifPublicShow">-->
<!--        <el-input-->
<!--          v-model="queryParams.ifPublicShow"-->
<!--          placeholder="请输入是否显示 默认0 不公开显示"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="标题" prop="title">-->
<!--        <el-input-->
<!--          v-model="queryParams.title"-->
<!--          placeholder="请输入标题"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="盈利百分比" prop="discount">-->
<!--        <el-input-->
<!--          v-model="queryParams.discount"-->
<!--          placeholder="请输入盈利百分比"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:tower:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:tower:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:tower:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:tower:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="towerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="塔名称" align="center" prop="title" />
      <el-table-column label="塔首页封面图" align="center" prop="imgUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="塔最高层数" align="center" prop="level" >
        <template slot-scope="scope">
          <el-tag > 塔总共{{scope.row.level}}层 </el-tag>
          <el-tag :type="scope.row.nowLevel === scope.row.level ?'success' : `danger`" > 已添加{{scope.row.nowLevel}}层 </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="奖池/出奖门槛" align="center" prop="menkan" />
      <el-table-column label="盈利" align="center" prop="profit" />
      <el-table-column label="塔状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">{{scope.row.status === 1 ? '已上架' : '已下架'}} </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="排序值" align="center" prop="sort" />
<!--      <el-table-column label="塔热度" align="center" prop="hotkey" />-->
<!--      <el-table-column label="区间暂时没用到" align="center" prop="goodsInterval" />-->
      <el-table-column label="是否公开显示" align="center" prop="ifPublicShow" >
        <template slot-scope="scope">
          <el-tag :type="scope.row.ifPublicShow === 1 ? 'success' : 'error'">{{scope.row.status === 1 ? '公开显示' : '充值可见'}} </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="盈利百分比" align="center" prop="discount" >
        <template slot-scope="scope">
          <el-tag > {{scope.row.discount*100}}% </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-eleme"
            @click="goToDetail(scope.row)"
            v-hasPermi="['system:tower:edit']"
          >新增塔层</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:tower:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:tower:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改爬塔实体对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="塔封面图">
          <image-upload v-model="form.imgUrl"/>
        </el-form-item>
        <el-form-item label="塔层数" prop="level">
          <el-input-number v-model="form.level"  placeholder="请输入塔层数"  />
        </el-form-item>

        <el-form-item label="初始奖池/出奖门槛" prop="menkan">
          <el-input-number v-model="form.menkan" placeholder="请输入门槛" />
        </el-form-item>

<!--        <el-form-item label="利润" prop="profit">-->
<!--          <el-input v-model="form.profit" placeholder="请输入利润" />-->
<!--        </el-form-item>-->
        <el-form-item label="盈利百分比" prop="discount">
          <el-input-number :precision="3"  :step="0.1"  :max="0.7" :min="0"  v-model="form.discount" placeholder="请输入盈利百分比" />
        </el-form-item>

        <el-form-item label="是否公开显示" prop="ifPublicShow">
          <el-switch v-model="form.ifPublicShow == 1"  @change="toggleSwitch" active-color="#13ce66" inactive-color="#ff4949" />
        </el-form-item>

        <el-form-item label="排序值" prop="sort">
          <el-input-number v-model="form.sort" placeholder="请输入排序值" />
        </el-form-item>


        <el-form-item label="状态" prop="sort">
          <el-switch v-model="form.status === 1" active-text="上架" inactive-text="下架"    @change="toggleSwitch2" active-color="#13ce66" inactive-color="#ff4949" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTower, getTower, delTower, addTower, updateTower } from "@/api/system/tower";

export default {
  dicts: ['box_status'],
  name: "Tower",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 爬塔实体表格数据
      towerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        menkan: null,
        profit: null,
        level: null,
        status: null,
        imgUrl: null,
        sort: null,
        price: null,
        hotkey: null,
        goodsInterval: null,
        ifPublicShow: null,
        title: null,
        discount: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ifPublicShow: [
          { required: true, message: "是否显示 默认0 不公开显示不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    toggleSwitch(value) {
      this.form.ifPublicShow = value ? 1 : 0; // 根据开关状态设置为 1 或 0
    },
    toggleSwitch2(value) {
      this.form.status = value ? 1 : 0; // 根据开关状态设置为 1 或 0
    },

    goToDetail(row) {
      this.$tab.openPage(
        '添加塔层',
        '/level/add/index/' + row.id
      )
    },

    /** 查询爬塔实体列表 */
    getList() {
      this.loading = true;
      listTower(this.queryParams).then(response => {
        this.towerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        menkan: null,
        profit: null,
        level: null,
        status: 0,
        imgUrl: null,
        sort: null,
        price: null,
        createTime: null,
        updateTime: null,
        hotkey: null,
        goodsInterval: null,
        ifPublicShow: 0,
        title: null,
        discount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加爬塔";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTower(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改爬塔";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTower(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTower(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除爬塔实体编号为"' + ids + '"的数据项？').then(function() {
        return delTower(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/tower/export', {
        ...this.queryParams
      }, `tower_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
